//
//  threeViewController.m
//  One Construction
//
//  Created by apple on 2019/9/3.
//  Copyright © 2019 apple. All rights reserved.
//

#import "threeViewController.h"
#import "settingViewController.h"
#import "helpViewController.h"
#import "faceToFaceViewController.h"
#import "playRecordViewController.h"
#import <ZBarSDK/ZBarSDK.h>
#import "ViewController.h"
#import "myCourceViewController.h"

@interface threeViewController ()<UITableViewDelegate,UITableViewDataSource,ZBarReaderDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)UIView *headView;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSArray *imgArr;
@property(nonatomic,strong)NSArray *titleArr;
@property(nonatomic,strong)NSString *scanString;
@property(nonatomic,strong)UIImageView *iconImgView;
@property(nonatomic,strong)UIButton *loginBtn;
@property(nonatomic,strong)myLable *userNameLable;
@property(nonatomic,strong)UIImagePickerController *pickController;
//0位点击的头像1位点击扫一扫
@property(nonatomic,assign)int status;


@end

@implementation threeViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self isLopgin];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.view.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
    self.title  = @"个人中心";

    self.titleArr = [[NSArray alloc] initWithObjects:@"帮助中心",@"扫一扫",@"我的面授课",@"设置", nil];
     self.imgArr = [[NSArray alloc]initWithObjects:@"help",@"scan",@"facetoface",@"setting", nil];
    [self setUpView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)setUpView{
    [self.view addSubview:self.headView];
    [self.headView addSubview:self.iconImgView];
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.headView.mas_centerX);
         make.centerY.mas_equalTo(self.headView.mas_centerY);
        make.width.height.mas_equalTo(90);
        
    }];
    if ([User sharedUUser].isLogin == YES) {
          [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:[User sharedUUser].user_imageViewName] placeholderImage:[UIImage imageNamed:@"Image"]];
    }
    else{
        self.iconImgView.image = [UIImage imageNamed:@"Image"];
    }
//    UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickIconImg)];
//
//    [self.iconImgView addGestureRecognizer:Tap];
//
//    [self.headView addSubview:self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.headView.mas_centerX);
         make.width.mas_equalTo(84);
         make.height.mas_equalTo(37);
        make.top.mas_equalTo(self.iconImgView.mas_bottom).mas_offset(11);
    }];
    [self.loginBtn addTarget:self action:@selector(clickLogin) forControlEvents:UIControlEventTouchUpInside];
    
    
     [self.headView addSubview:self.userNameLable];
    [self.userNameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.headView.mas_centerX);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(37);
make.top.mas_equalTo(self.iconImgView.mas_bottom).mas_offset(11);
    }];
    [self isLopgin];
    
     [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headView.mas_bottom).mas_offset(50);
        make.left.mas_offset(10);
         make.right.mas_offset(-10);
        make.height.mas_equalTo(200);
        
    }];
    if (@available (iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

    
    
}

- (void)clickLogin{
    
    ViewController *vc = [[ViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

- (void)isLopgin{
    if ([User sharedUUser].isLogin ==YES) {
        self.loginBtn.hidden = YES;
        self.userNameLable.hidden = NO;
        
    }
    else{
        self.loginBtn.hidden = NO;
        self.userNameLable.hidden = YES;
        
        
        
    }
    
    
}
- (void)clickIconImg{
    if ([User sharedUUser].isLogin==YES) {
        self.status = 0;
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"上传头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *fromAlbum = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.pickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.pickController animated:YES completion:nil];
        }];
        
        UIAlertAction *fromPhoto = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.pickController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.pickController animated:YES completion:nil];    }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [alertC addAction:fromPhoto];
        [alertC addAction:fromAlbum];
        [alertC addAction:cancel];
        [self presentViewController:alertC animated:YES completion:nil];
    }
    else{
         [ProgressHUD showText:@"请先登录" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
        
    }
    
    
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    if (self.status==0) {
        UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage];
        
        if (!img) {
            
        }
        
        else{
            self.iconImgView.image = img;
            
        }
        
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
    else if (self.status ==1){
        id<NSFastEnumeration> results =
        [info objectForKey: ZBarReaderControllerResults];
        ZBarSymbol *symbol = nil;
        for(symbol in results)
            // EXAMPLE: just grab the first barcode
            break;
        
        // EXAMPLE: do something useful with the barcode data
        
        _scanString = symbol.data;
        // ADD: dismiss the controller (NB dismiss from the *reader*!)
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        [self creatAlert];
        
    }
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _imgArr.count
    ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *detailcell = @"detailcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailcell];
    UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 16, 19, 19)];
    [cell.contentView addSubview:leftImage];
    leftImage.image = [UIImage imageNamed:_imgArr[indexPath.row]];
    
    myLable *rightLable = [[myLable alloc]initWithFrame:CGRectMake(leftImage.frame.origin.x+leftImage.frame.size.width+18, 14,200, 23)];
    [rightLable setLabletextColor:[UIColor blackColor] Text:_titleArr[indexPath.row] TextFont:16];
    [cell.contentView addSubview:rightLable];
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row ==0) {
        if ([User sharedUUser].isLogin ==YES) {
            helpViewController *help = [[helpViewController alloc]init];
            [self.navigationController pushViewController:help animated:YES];
        }
        else{
            
            [ProgressHUD showText:@"请先登录" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
        }
    }
    else if (indexPath.row ==1){
      
        
        if ([User sharedUUser].isLogin == YES) {

            self.status =1;
            ZBarReaderViewController *reader = [ZBarReaderViewController new];
            reader.readerDelegate = self;
            reader.supportedOrientationsMask = ZBarOrientationMaskAll;
            
            ZBarImageScanner *scanner = reader.scanner;
            [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
            reader.showsZBarControls = YES;
            [self presentViewController:reader animated:YES completion:nil];

        }
        else{

            [ProgressHUD showText:@"请先登录" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
        }
    }
    else if (indexPath.row ==2){
        
       faceToFaceViewController  *face = [[faceToFaceViewController alloc]init];
        [self.navigationController pushViewController:face animated:YES];
    }
    else if (indexPath.row ==3){
        settingViewController  *set = [[settingViewController alloc]init];
        [self.navigationController pushViewController:set animated:YES];
    }
    
}
//扫描结果
- (void)creatAlert{
    if ([judgeString isBlankString:_scanString]) {
        [ProgressHUD showText:@"扫描失败，请重新扫描" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
        
    }
    else{
        
        
        [self presentViewController:[UIAlertController alertControllerWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"可前往:%@",_scanString] optionStyle:OptionStyleStyleOK_Cancel OkTitle:@"复制" cancelTitle:@"取消" okBlock:^{
            UIPasteboard *pad = [UIPasteboard generalPasteboard];
            [pad setString:_scanString];
            if (pad== nil) {
                [ProgressHUD showText:@"复制失败" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
            }
            else{
                 [ProgressHUD showText:@"复制成功" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
            }
            
        } cancelBlock:^{
            
        }] animated:YES completion:^{
            
        }];
        
    }
    
    
    
}
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"detailcell"];
        _tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _tableview.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
        _tableview.layer.cornerRadius = 10;
        _tableview.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.05].CGColor;
        _tableview.layer.shadowOffset = CGSizeMake(0,1);
        _tableview.layer.shadowOpacity = 1;
        _tableview.layer.shadowRadius = 2;
        _tableview.bounces = NO;
    }
    
    return _tableview;
}
- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, -NavBarSafetyZone, ScreenWidth, 398)];
        _headView.backgroundColor =  [UIColor colorWithRed:31/255.0 green:78/255.0 blue:196/255.0 alpha:1.0];
        
        UIView *twoBtnView = [[UIView alloc]init];
        [_headView addSubview:twoBtnView];
        [twoBtnView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(10);
             make.right.mas_offset(-10);
             make.bottom.mas_offset(28);
            make.height.mas_equalTo(62);
        }];
        twoBtnView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
        twoBtnView.layer.cornerRadius = 10;
        twoBtnView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.05].CGColor;
        twoBtnView.layer.shadowOffset = CGSizeMake(0,1);
        twoBtnView.layer.shadowOpacity = 1;
        twoBtnView.layer.shadowRadius = 2;
        
        UIButton *leftBtn = [[UIButton alloc]init];
        [twoBtnView addSubview:leftBtn];

        [leftBtn setImage:[UIImage imageNamed:@"mycource"] forState:UIControlStateNormal];
        [leftBtn setTitle:@"我的课程" forState:UIControlStateNormal];
        [leftBtn.titleLabel setFont:[UIFont systemFontOfSize:10]];
//         [leftBtn.titleLabel setFont:[UIFont fontWithName:@"PingFang-SC-Medium" size: 13*IP6ORIENTED]];
        [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        leftBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
       [leftBtn layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleImageTop imageTitlespace:25];
       
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(57);
            make.top.mas_offset(6);
            make.bottom.mas_offset(0);
            make.width.mas_equalTo(74);
        }];
        [leftBtn addTarget:self action:@selector(clickMyCource) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        UIButton *rightBtn = [[UIButton alloc]init];
        [twoBtnView addSubview:rightBtn];
        
        [rightBtn setImage:[UIImage imageNamed:@"record"] forState:UIControlStateNormal];
        [rightBtn setTitle:@"播放记录" forState:UIControlStateNormal];
        [rightBtn.titleLabel setFont:[UIFont systemFontOfSize:10]];
        //         [leftBtn.titleLabel setFont:[UIFont fontWithName:@"PingFang-SC-Medium" size: 13*IP6ORIENTED]];
        [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        rightBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [rightBtn layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleImageTop imageTitlespace:20];
        
        [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_offset(-57);
            make.top.mas_offset(6);
            make.bottom.mas_offset(0);
            make.width.mas_equalTo(74);
        }];
        
        [rightBtn addTarget:self action:@selector(clickRecordBtn) forControlEvents:UIControlEventTouchUpInside];
    
        
        
    }
    return _headView;
}
- (void)clickRecordBtn{
    
    if ([User sharedUUser].isLogin==YES) {
        playRecordViewController *re =[[playRecordViewController alloc]init];
        [self.navigationController pushViewController:re animated:YES];
        
    }
    else{
        [ProgressHUD showText:@"请先登录" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
    }
    
    
}
- (void)clickMyCource{
    
    if ([User sharedUUser].isLogin==YES) {
//         *re =[[playRecordViewController alloc]init];
//        [self.navigationController pushViewController:re animated:YES];
        myCourceViewController *myc =[[myCourceViewController alloc]init];
         [self.navigationController pushViewController:myc animated:YES];
        
    }
    else{
        [ProgressHUD showText:@"请先登录" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
    }
    
    
}
- (UIImageView *)iconImgView{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc]init];
        _iconImgView.userInteractionEnabled  = YES;
        _iconImgView.layer.cornerRadius = 45;
        _iconImgView.layer.masksToBounds = YES;
    }
    return _iconImgView;
}
-(UIImagePickerController *)pickController{
    
    if (!_pickController) {
        _pickController = [[UIImagePickerController alloc]init];
        _pickController.delegate = self;
        _pickController.navigationBarHidden = NO;
        _pickController.editing = NO;
        
    }
    
    return _pickController;
    
}
- (UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [[UIButton alloc]init];
        
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_loginBtn setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _loginBtn;
}
- (myLable *)userNameLable{
    if (!_userNameLable) {
        _userNameLable = [[myLable alloc]init];
        _userNameLable.text = [User sharedUUser].user_name;
        _userNameLable.textColor = [UIColor whiteColor];
         _userNameLable.textAlignment = NSTextAlignmentCenter;
        _userNameLable.font = [UIFont systemFontOfSize:13];
    }
    return _userNameLable;
}
@end
